Imports System.Data

Partial Class pages_Payments_default : Inherits CPage

#region "Querystring (Filters)"
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property
    
    'Rename or Delete:
    Public ReadOnly Property OrderId As Integer
        Get
            Return CWeb.RequestInt("orderId")
        End Get
    End Property
    Public ReadOnly Property CustomerId As Integer
        Get
            Return CWeb.RequestInt("customerId")
        End Get
    End Property
    Public ReadOnly Property ProductId As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
#End Region

#Region "Members"
    Private m_payments As CPaymentList
#End Region

#Region "Data"
    Public ReadOnly Property [Payments]() As CPaymentList
        Get
            If IsNothing(m_payments) Then
                m_payments = New CPayment().SelectSearch(ctrlPayments.PagingInfo, txtSearch.Text, OrderId, CustomerId, ProductId) 'Sql-based Paging
                'm_payments.PreloadChildren() 'Loads children for page in one hit (where applicable)
            End If
            Return m_payments
        End Get
    End Property
    Public Function PaymentsAsDataset() As System.Data.DataSet
        Return New CPayment().SelectSearch_Dataset(txtSearch.Text, OrderId, CustomerId, ProductId)
    End Function
#End Region

#Region "Event Handlers - Page "
    Protected Overrides Sub PageInit()
        'Populate Dropdowns

        'Side Menu
        If OrderId > 0 Then
            UnbindSideMenu()
            PageTitle &= " for Order"
            MenuSelected = "Orders"
            AddMenuSide("Orders", CSitemap.Orders)
            AddMenuSide("Order Details", CSitemap.Order(OrderId))
            'AddMenuSide("Order Reminders", CSitemap.Reminders(OrderId))
            AddMenuSide("Order Payments")
        ElseIf CustomerId > 0 Then
            UnbindSideMenu()
            MenuSelected = "Customers"
            PageTitle &= " for " & New CCustomer(CustomerId).CustomerName
            AddMenuSide("Customers", CSitemap.Customers)
            AddMenuSide("Customer Details", CSitemap.Customer(CustomerId))
            AddMenuSide("Customer Orders", CSitemap.OrdersForCustomer(CustomerId))
            'AddMenuSide("Customer Reminders", CSitemap.Reminders(CustomerId))
            AddMenuSide("Customer Payments")
        ElseIf ProductId > 0 Then
            ddCurrent.Visible = True

            UnbindSideMenu()
            PageTitle &= " for " & CProduct.Cache.GetById(ProductId).ProductName
            MenuSelected = "Products"
            With CProduct.Cache.GetById(ProductId)
                AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, .ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
                AddMenuSide(.TypeName & " Details", CSitemap.ProductEdit(ProductId))
                AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId))
                If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
                If .CanHaveTranscript Then
                    AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId))
                End If
                If .CanHaveFiles Then
                    AddMenuSide("Dropbox Files", CSitemap.ProductFiles(ProductId))
                Else
                    AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", CFile.Cache.GetByProductId(Me.ProductId)), CSitemap.ProductFiles(ProductId))
                End If
                If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                    AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId))
                    AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId), True)
                End If
            End With
        End If


        'Search state (from querystring)
        txtSearch.Text = Me.Search

        'Display Results
        ctrlPayments.Display(Me.Payments, New CPayment.CTotals(Search, CustomerId, ProductId, OrderId))

        'Client-side
        Me.Form.DefaultFocus = txtSearch.ClientID
        Me.Form.DefaultButton = btnSearch.UniqueID  'CTextbox.OnReturnPress(txtSearch, btnSearch)




        'If OrderId > 0 Then
        '    With New COrder(OrderId)
        '        If .OrderIsPaid Then
        '            btnSearch.Visible = False
        '            txtSearch.Visible = False
        '        Else
        '            btnSearch.Text = "Enter a Cash Payment"
        '            CTextbox.ValidateMoney(txtSearch)
        '            CTextbox.RightAlign(txtSearch)
        '            CTextbox.SetMoney(txtSearch, .OrderPriceHistorical)
        '            btnSearch.OnClientClick = CWeb.Confirm("Do you want to record receipt of a cash payment, and mark this Order as paid?")
        '            Me.Form.DefaultFocus = String.Empty
        '        End If
        '    End With
        'End If
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Response.Redirect(CSitemap.Payments(txtSearch.Text, OrderId, CustomerId, ProductId))
        'If OrderId > 0 Then
        '    Dim order As New COrder(OrderId)
        '    If order.OrderIsPaid Then Exit Sub

        '    With New CPayment
        '        .Order = order
        '        .PaymentAmount = CTextbox.GetMoney(txtSearch)
        '        .PaymentFee = 0
        '        .PaymentCode = "CASH"
        '        .PaymentFirstName = CUser.Current.UserFirstName
        '        .PaymentLastName = CUser.Current.UserLastName
        '        .PaymentIsOk = True
        '        .Save()
        '    End With
        '    order.Reload(Nothing)
        '    If order.PaidAmount >= order.OrderPriceHistorical Then
        '        order.OrderIsPaid = True
        '        order.Save()
        '    End If
        '    Response.Redirect(Request.RawUrl)
        'Else
        '    Response.Redirect(CSitemap.Payments(txtSearch.Text, OrderId, CustomerId, ProductId))
        'End If
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_ExportClick() Handles ctrlPayments.ExportClick
        CDataSrc.ExportToCsv(PaymentsAsDataSet, Response, "Payments.csv")
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlPayments.ResortClick
        Response.Redirect(CSitemap.Payments(txtSearch.Text, OrderId, CustomerId, ProductId, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
